package com.googlecode.scup.grammar.grammarspart;

import java.util.List;
import java.util.ArrayList;

public class AutomatRule {
    private Symbol base = null;
    private Item item = null;

    private AutomatRule(Symbol base, Item item) {
        this.base = base;
        this.item = item;
    }

    static public List<AutomatRule> createAutomatRules(Rule rule){
        List<AutomatRule> result = new ArrayList<AutomatRule>();
        Symbol base = rule.getBase();
        for (Production production : rule.getProductions()) {
            result.add(new AutomatRule(base, new Item(production)));
        }
        return result;
    }

    public Symbol getBase() {
        return base;
    }

    public Item getItem() {
        return item;
    }

    public Symbol getPointSymbol() {
        return item.getPointSymbol();
    }

    public int getSizeOfItem() {
        return item.size();
    }

    public Symbol getLastSymbol() {
        return item.getSymbol(item.size() - 1);
    }

    public AutomatRule getNext() {
        return new AutomatRule(base, item.getNextProduction());
    }

    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append(base.getName());
        result.append(" ->");
        result.append(item);
        return result.toString();
    }

    public boolean equals(Object obj) {
        if (! (obj instanceof AutomatRule)) {
            return false;
        }
        AutomatRule automatRule = (AutomatRule) obj;
        if (!this.base.equals(automatRule.getBase())) return false;
        return this.item.equals(automatRule.getItem());
    }
}
